<?php

//公告管理
class AnnouncementsController extends Controller
{

    public function actionIndex()
    {
        $this->render('index');
    }

    public function actionGetdata()
    {

        $page = $_REQUEST['page'];
        $rows = $_REQUEST['rows'];
        $start = ($page - 1) * $rows;
        $total = $this->connection->createCommand("select count(1) from crm_announcements where deleted = 0 ")->queryScalar();
        $result = $this->connection->createCommand("select * from crm_announcements  where deleted = 0  limit {$rows}
 offset {$start}")->queryAll();

        $data = Array();
        $data['total'] = $total;
        $data['rows'] = $result;

        echo json_encode($data);
    }

    public function actionShowedit()
    {

        $announcements_id = $_REQUEST['announcements_id'];

        $announcements = null;
        if ($announcements_id == 0) {
            //新增
        } else {
            //编辑
            $announcements = $this->connection->createCommand("select * from crm_announcements where announcements_id = :announcements_id")
                ->bindParam(':announcements_id', $announcements_id, PDO::PARAM_INT)->queryRow();
        }

        $this->render('showedit', ['announcements' => $announcements]);
    }

    public function actionSavedata()
    {

        $announcements_id = $_REQUEST['announcements_id'];
        $title = $_REQUEST['title'];
        $content = $_REQUEST['content'];
        $announcements_type = $_REQUEST['announcements_type'];
        $start_time = $_REQUEST['start_time'];
        $end_time = $_REQUEST['end_time'];

        if ($announcements_id != null) {
            //编辑
            $this->connection->createCommand("update crm_announcements set title = :title,announcements_type = :announcements_type,start_time=:start_time,
end_time=:end_time,content=:content 
where announcements_id = :announcements_id")
                ->bindParam(':announcements_id', $announcements_id, PDO::PARAM_INT)
                ->bindParam(':title', $title, PDO::PARAM_STR)
                ->bindParam(':announcements_type', $announcements_type, PDO::PARAM_STR)
                ->bindParam(':start_time', $start_time, PDO::PARAM_STR)
                ->bindParam(':end_time', $end_time, PDO::PARAM_STR)
                ->bindParam(':content', $content, PDO::PARAM_STR)
                ->execute();
        } else {
            //添加
            $this->connection->createCommand("insert into crm_announcements (title,announcements_type,start_time,end_time,content) values 
(:title,:announcements_type,:start_time,:end_time,:content ) ")
                ->bindParam(':title', $title, PDO::PARAM_STR)
                ->bindParam(':announcements_type', $announcements_type, PDO::PARAM_STR)
                ->bindParam(':start_time', $start_time, PDO::PARAM_STR)
                ->bindParam(':end_time', $end_time, PDO::PARAM_STR)
                ->bindParam(':content', $content, PDO::PARAM_STR)
                ->execute();
        }
    }

    public function actionRemove()
    {
        $announcements_id = $_REQUEST['announcements_id'];
        $this->connection->createCommand("update crm_announcements set deleted = 1 
where announcements_id = :announcements_id")
            ->bindParam(':announcements_id', $announcements_id, PDO::PARAM_INT)
            ->execute();
    }

    public function actionView()
    {

        $announcements_id = $_REQUEST['announcements_id'];
        $announcements = $this->connection->createCommand("select * from crm_announcements where announcements_id = :announcements_id")->bindParam(':announcements_id', $announcements_id, PDO::PARAM_INT)->queryRow();
        $this->render('view', ['announcements' => $announcements]);
    }

}